package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.SignIn;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.service.ISignInService;
import cn.wolfcode.trip.base.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@RestController
@RequestMapping("signIn")
public class SignInController {
    @Autowired
    private ISignInService signInService;

    //保存
    @PostMapping("save")
    public JSONResult save(SignIn signIn){
        signInService.save(signIn);
        return new JSONResult();
    }
    //查询是否已签到
    @GetMapping
    public SignIn getData(SignIn signIn){

        SignIn s = signInService.confirm(signIn);
        return s;
    }
    //查询签到天数
    @GetMapping("/day")
    public Long getDay(Long userId){
        Long days = signInService.getDay(userId);
       return days;
    }
    //查询某一用户已签到数据
    @GetMapping("/all")
    public List<SignIn> getAll(Long userId){
        List<SignIn> list = signInService.listAll(userId);
        return list;
    }
    //查询连续的签到数据
    @GetMapping("/series")
    public Integer getLongDay(Long userId){
        //查询某一用户已签到数据的日期
        //获得用户的日期的list
        List<Long> dateList = new ArrayList<>();
        dateList = signInService.listAllDate(userId);

        List<Integer> list = new ArrayList<>();

        //将Long转换Integer
        for(int i = 0;i<dateList.size();i++){
           Integer ele = dateList.get(i).intValue();
           list.add(ele);
        }

        //定义两个数组
        List<Integer> list2 = new ArrayList<Integer>();
        List<Integer> list3 = new ArrayList<Integer>();
        for (int i = 0; i < list.size(); i++ ) {
            if (i == list.size() - 1) {
                break;
            } else if (list.get(i + 1) - list.get(i) != 1) {
                list2.add(i);
            }
        }
        //通过判断获取连续的个数，在list3中取最大值即可。
        if (0 == list2.size()) {
            return list.size();
        } else {
            for (int i = 0; i < list2.size(); i++ ) {
                if (1 == list2.size()) {
                    list3.add(list2.get(0) + 1);
                    list3.add(list.size() - 1 - list2.get(i));
                } else {
                    if (i == 0) {
                        list3.add(list2.get(0) + 1);
                        list3.add(list2.get(i + 1) - list2.get(i));
                    } else if (i == list2.size() - 1) {
                        list3.add(list.size() - 1 - list2.get(i));
                    } else {
                        list3.add(list2.get(i + 1) - list2.get(i));
                    }
                }
            }
            return Collections.max(list3);
        }



    }


}
